package org.jz.gae.redbook.spb.client.slate.location;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.jz.gae.redbook.spb.client.slate.district.DistrictChoosePlate;
import org.jz.gae.redbook.spb.client.slate.species.SpeciesChoosePlate;
import org.jz.gae.redbook.spb.client.toolkit.Slate;
import org.jz.gae.redbook.spb.shared.DistrictEntityView;
import org.jz.gae.redbook.spb.shared.LocationEntityView;
import org.jz.gae.redbook.spb.shared.SpeciesEntityView;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class LocationPropertiesPanel extends Composite 
{
	  private String locationDistrictKey;
	  private String locationSpeciesKey;
      private double locationCoordLatitude;
      private double locationCoordLongitude;
	  private final Button locationDistrictChooseButton = new Button("...");
	  private final Button locationSpeciesChooseButton = new Button("...");
	  private final Button locationCoordsApplyButton = new Button("+");
	  private final TextBox locationWorkName = new TextBox();
	  private final TextBox locationDistrictPresentation = new TextBox();
	  private final TextBox locationSpeciesPresentation = new TextBox();
	  private final TextArea locationInitialData = new TextArea();
	  private final TextArea locationDescription = new TextArea();
	  private final TextArea locationCenose = new TextArea();
	  private final TextArea locationSpecificExistingThreats = new TextArea();
	  private final TextArea locationSpecificPotentialThreats = new TextArea();
	  private final TextArea locationNote = new TextArea();
	  private final TextBox locationCoordLat = new TextBox();
	  private final TextBox locationCoordLong = new TextBox();
	  private final FlexTable detailsTable;
	  
	  private LocationCoordChangeListener locationCoordChangeListener;
	  
	  private Slate slate;
	  
	  public interface LocationCoordChangeListener
	  {
		  public void locationCoordsChanged(
				  double _Latitude,
				  double _Longitude);
	  }
	  
	  public LocationPropertiesPanel()
	  {
		  SimplePanel contentDetailsDecorator = new SimplePanel();
	    contentDetailsDecorator.setWidth("18em");
	    initWidget(contentDetailsDecorator);

	    VerticalPanel contentDetailsPanel = new VerticalPanel();
	    contentDetailsPanel.setWidth("100%");

	    // Create the contacts list
	    //
	    detailsTable = new FlexTable();
	    detailsTable.setCellSpacing(0);
	    detailsTable.setWidth("100%");
	    detailsTable.addStyleName("contacts-ListContainer");
	    detailsTable.getColumnFormatter().addStyleName(1, "add-contact-input");
	    initDetailsTable();
	    contentDetailsPanel.add(detailsTable);
	    
	    contentDetailsDecorator.add(contentDetailsPanel);
	  }
	  
	  private void initDetailsTable() 
	  {
	    detailsTable.setWidget(0, 0, new Label("Рабочее название:"));
	    detailsTable.setWidget(0, 2, locationWorkName);
	    detailsTable.setWidget(0, 3, new Label("Начальные сведения:"));
	    detailsTable.setWidget(0, 4, locationInitialData);
	    detailsTable.setWidget(1, 0, new Label("Вид:"));
	    detailsTable.setWidget(1, 2, locationSpeciesPresentation);
	    detailsTable.setWidget(1, 3, locationSpeciesChooseButton);
	    detailsTable.setWidget(2, 0, new Label("Район:"));
	    detailsTable.setWidget(2, 2, locationDistrictPresentation);
	    detailsTable.setWidget(2, 3, locationDistrictChooseButton);
	    detailsTable.setWidget(4, 0, new Label("Описание:"));
	    detailsTable.setWidget(4, 1, new Label("Привязка:"));
	    detailsTable.setWidget(4, 2, locationDescription);
	    detailsTable.setWidget(4, 3, new Label("Биоценоз:"));
	    detailsTable.setWidget(4, 4, locationCenose);
	    detailsTable.setWidget(5, 0, new Label("Угрозы:"));
	    detailsTable.setWidget(5, 1, new Label("Существующие:"));
	    detailsTable.setWidget(5, 2, locationSpecificExistingThreats);
	    detailsTable.setWidget(5, 3, new Label("Потенциальные угрозы:"));
	    detailsTable.setWidget(5, 4, locationSpecificPotentialThreats);
	    detailsTable.setWidget(6, 0, new Label("Примечания:"));
	    detailsTable.setWidget(6, 2, locationNote);
	    detailsTable.setWidget(7, 0, new Label("Координаты:"));
	    detailsTable.setWidget(7, 1, new Label("Широта:"));
	    detailsTable.setWidget(7, 2, locationCoordLat);
	    detailsTable.setWidget(7, 3, new Label("Долгота:"));
	    detailsTable.setWidget(7, 4, locationCoordLong);
	    detailsTable.setWidget(5, 5, locationCoordsApplyButton);
	    
	    locationWorkName.setFocus(true);
	    
	    locationDistrictPresentation.setEnabled(false);
	    locationSpeciesPresentation.setEnabled(false);
	    
	    locationDistrictChooseButton.addClickHandler(new ClickHandler(){
	    	@Override
	    	public void onClick(ClickEvent event)
	    	{
	    		chooseDistrict();
	    	}
	    });
	    
	    locationSpeciesChooseButton.addClickHandler(new ClickHandler(){
	    	@Override
	    	public void onClick(ClickEvent event)
	    	{
	    		chooseSpecies();
	    	}
	    });
	    
	    locationCoordsApplyButton.addClickHandler(new ClickHandler(){
	    	@Override
	    	public void onClick(ClickEvent event)
	    	{
	    		applyLocationCoords();
	    	}
	    });
	  }

	public LocationEntityView pickUpInformation()
	{
		LocationEntityView location = new LocationEntityView();
		/**/
		location.setLocationDistrictKey(locationDistrictKey);
		location.setLocationDistrictPresentation(locationDistrictPresentation.getValue());
		location.setLocationSpeciesKey(locationSpeciesKey);
		location.setLocationSpeciesPresentation(locationSpeciesPresentation.getValue());
		location.setLocationWorkName(locationWorkName.getValue());
		location.setLocationInitialData(locationInitialData.getValue());
		location.setLocationDescription(locationDescription.getValue());
		location.setLocationCenose(locationCenose.getValue());
		location.setLocationCoordLatitude(locationCoordLatitude);
		location.setLocationCoordLongitude(locationCoordLongitude);
		location.setLocationCoordLat(new BigDecimal(locationCoordLat.getValue()).setScale(5, RoundingMode.HALF_UP));
		location.setLocationCoordLong(new BigDecimal(locationCoordLong.getValue()).setScale(5, RoundingMode.HALF_UP));
		location.setLocationSpecificExistingThreats(locationSpecificExistingThreats.getValue());
		location.setLocationSpecificPotentialThreats(locationSpecificPotentialThreats.getValue());
		location.setLocationNote(locationNote.getValue());
		return location;
	}
	
	public void displayInformation(
			LocationEntityView _Location)
	{
		locationDistrictKey = _Location.getLocationDistrictKey();
		locationDistrictPresentation.setValue(_Location.getLocationDistrictPresentation());
		locationSpeciesKey = _Location.getLocationSpeciesKey();
		locationSpeciesPresentation.setValue(_Location.getLocationSpeciesPresentation());
		locationWorkName.setValue(_Location.getLocationWorkName());
		locationInitialData.setValue(_Location.getLocationInitialData());
		locationDescription.setValue(_Location.getLocationDescription());
		locationCenose.setValue(_Location.getLocationCenose());
		locationCoordLatitude = _Location.getLocationCoordLatitude();
		locationCoordLongitude = _Location.getLocationCoordLongitude();
		locationCoordLat.setValue(_Location.getLocationCoordLat().toPlainString());
		locationCoordLong.setValue(_Location.getLocationCoordLong().toPlainString());
		locationSpecificExistingThreats.setValue(_Location.getLocationSpecificExistingThreats());
		locationSpecificPotentialThreats.setValue(_Location.getLocationSpecificPotentialThreats());
		locationNote.setValue(_Location.getLocationNote());
	    locationNote.setValue(_Location.getLocationNote());
	}
	
	public void lockInterface()
	{
		locationDistrictPresentation.setEnabled(false);
		locationSpeciesPresentation.setEnabled(false);
		locationWorkName.setEnabled(false);
		locationInitialData.setEnabled(false);
		locationDescription.setEnabled(false);
		locationCenose.setEnabled(false);
		locationCoordLat.setEnabled(false);
		locationCoordLong.setEnabled(false);
		locationSpecificExistingThreats.setEnabled(false);
		locationSpecificPotentialThreats.setEnabled(false);
	    locationNote.setEnabled(false);
	    /**/
	    locationDistrictChooseButton.setEnabled(false);
	    locationDistrictChooseButton.setVisible(false);
	    /**/
	    locationSpeciesChooseButton.setEnabled(false);
	    locationSpeciesChooseButton.setVisible(false);
	    /**/
	    locationCoordsApplyButton.setEnabled(false);
	    locationCoordsApplyButton.setVisible(false);
	}
	
	
	public void setSlate(Slate slate)
	{
		this.slate = slate;
	}
	
	public Slate getSlate()
	{
		return slate;
	}	
	
	private void chooseDistrict()
	{
		try
		{
			DistrictChoosePlate plate = new DistrictChoosePlate();
			plate.chooseDistrict(slate, new DistrictChoosePlate.DistrictChoosingListener()
			{
				
				@Override
				public void districtChoosen(
						DistrictEntityView _District)
				{
					System.err.println("district chosen : " + _District.getDistrictKey());
					locationDistrictKey = _District.getDistrictKey();
					locationDistrictPresentation.setValue(_District.getDistrictRusName());
				}
			});
		}
		catch(Exception ex)
		{
			Window.alert("Error : " + ex.getMessage());
		}
	}

	private void chooseSpecies()
	{
		try
		{
			SpeciesChoosePlate plate = new SpeciesChoosePlate();
			plate.chooseSpecies(slate, new SpeciesChoosePlate.SpeciesChoosingListener()
			{
				
				@Override
				public void speciesChoosen(
						SpeciesEntityView _Species)
				{
					System.err.println("species chosen : " + _Species.getSpeciesKey());
					locationSpeciesKey = _Species.getSpeciesKey();
					locationSpeciesPresentation.setValue(_Species.getSpeciesLatName());
				}
			});
		}
		catch(Exception ex)
		{
			Window.alert("Error : " + ex.getMessage());
		}
	}

	public void setLocationCoords(
			LatLng _NewCoords)
	{
		locationCoordLatitude = _NewCoords.getLatitude();
		locationCoordLongitude = _NewCoords.getLongitude();
		locationCoordLat.setValue(locationCoordLatitude + "");
		locationCoordLong.setValue(locationCoordLongitude + "");
	}
	
	public void applyLocationCoords()
	{
		BigDecimal coordLatitude = new BigDecimal(locationCoordLat.getValue()).setScale(5, RoundingMode.HALF_UP);
		BigDecimal coordLongitude = new BigDecimal(locationCoordLong.getValue()).setScale(5, RoundingMode.HALF_UP);
		locationCoordLatitude = coordLatitude.doubleValue();
		locationCoordLongitude = coordLongitude.doubleValue();
		if (locationCoordChangeListener != null)
		{
		    locationCoordChangeListener.locationCoordsChanged(locationCoordLatitude, locationCoordLongitude);
		}
	}
	
	public LocationCoordChangeListener getLocationCoordChangeListener()
	{
		return locationCoordChangeListener;
	}
	
	public void setLocationCoordChangeListener(
			LocationCoordChangeListener locationCoordChangeListener)
	{
		this.locationCoordChangeListener = locationCoordChangeListener;
	}
}
